<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
     //封装链表类
    function LinkedList(){
        //内部的类:节点类
        function Node(data){
            this.data  = data
            this.next = null
        }

        //属性
        this.head =null
        this.length = 0

        //1.追加方法
        LinkedList.prototype.append = function (data) {
                // 1.创建新的节点
               var newNode = new Node(data);

                //2.判断是否添加的是第一个节点
              if(this.length == 0){  //是第一个节点
                      this.head = newNode;
              }else{
                      var current = this.head

                      while(current.next){
                          current = current.next
                      }

                      current.next = newNode
              }
              this.length +=1;
        }
        LinkedList.prototype.toString = function () {
             // 1.定义变量
             var current = this.head
             var listString = ""

             //2.循环获取每一个节点
             while(current){
                  listString += current.data + " "
                  current = current.next
             }
             return listString
        }
        LinkedList.prototype.insert = function (position,data) {
           //判断position是否越界
            if(position < 0 || position > this.length) return false

            //根据data创建node
            var newNode = new Node(data)

            // 判断插入的位置是否是第一个
            if(position == 0){
                newNode.next = this.head
                this.head = newNode
            } else {
                var index = 0
                var current = this.head
                var previous = null
                while (index++ < position){
                    previous = current
                    current = current.next
                }
                newNode.next = current
                previous.next = newNode

            }
            this.length += 1;
            return true;

        }

        LinkedList.prototype.get = function (position) {
            //1.越界判断
            if (position < 0 || position >= this.length) return null

            //2.获取对应的data
            var current =this.head
            var index = 0
            while (index++ < position) {
                current = current.next
            }

            return current.data
        }
        LinkedList.prototype.indexOf = function (data) {
             //定义变量
            var current = this.head
            var index = 0

            while (current){
                 if(current.data == data){
                     return index
                 }
                 current= current.next
                index += 1;
            }
            return -1;
        }
        LinkedList.prototype.update =function (position,newData) {
            //判断越界
            if(position < 0 || position >= this.length) return false

            //查找当前节点
            var current = this.head
            var index=0
            while (index++ < position){
                 current = current.next
            }
           current.data = newData

            return true
        }
        LinkedList.prototype.removeAt = function (position) {
               // 越界判断
             if(position < 0 || position >= this.length) return null

               //判断是否是第一个节点
            var current = this.head

            if(position == 0){
                this.head = this.head.next
            }else{
                var index = 0
                var previous = null
                while(index++ < position){
                    previous = current
                    current = current.next
                }
                //前一个节点的next指向,current的next即可
                 previous.next = current.next
            }
            // length - 1
            this.length -= 1

            return  current.data

        }
        LinkedList.prototype.remove  =function (data) {
               var index = this.indexOf(data);

               return this.removeAt(index)
        }

    }
  var list =  new LinkedList();
    list.append('abc');
     list.append('bbc');
     list.append('dbc');
    // alert(list.toString());
    list.insert(3,'cbc')
    alert(list.get(3))

</script>
</body>
</html>